package org.jeecg.modules.performance.job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.performance.service.ICalculatePerforService;
import org.jeecg.modules.publish.entity.ThirdContentLink;
import org.jeecg.modules.publish.entity.ThirdReadNum;
import org.jeecg.modules.publish.service.IThirdContentLinkService;
import org.jeecg.modules.publish.service.IThirdReadNumService;
import org.jeecg.modules.third.entity.ThirdAccount;
import org.jeecg.modules.third.entity.ThirdInfo;
import org.jeecg.modules.third.service.IThirdAccountService;
import org.jeecg.modules.third.service.IThirdInfoService;
import org.jeecg.util.DateTimeUtil;
import org.jeecg.util.DouYinUtil;
import org.jeecg.util.WeiBoUtil;
import org.jeecgframework.p3.core.util.HttpUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class PerformanceWeiBoReadJob implements Job {
    //
    @Autowired
    private IThirdContentLinkService thirdContentLinkService;
    @Autowired
    private IThirdReadNumService thirdReadNumService;
    @Autowired
    private IThirdInfoService thirdInfoService;
    @Autowired
    private IThirdAccountService thirdAccountService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String startTime = DateTimeUtil.getLastDayCharN8En(30 );
        String endTime = DateTimeUtil.getLastDayCharN8En(1);
        QueryWrapper<ThirdContentLink> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("data",startTime);
        queryWrapper.le("data",endTime);
        queryWrapper.eq("type",2);
        List<ThirdContentLink> list = thirdContentLinkService.list(queryWrapper);


       // Map<String, String> collect = list.stream().collect(Collectors.toMap(ThirdContentLink::get, ThirdContentLink::getContentId));
        Map<String, List<ThirdContentLink>> accountcollect = list.stream().collect(Collectors.groupingBy(x->x.getAccountId()));

        for (String s : accountcollect.keySet()) {
            List<String> thirdIs = new ArrayList<>();
            List<ThirdContentLink> thirdContentLinks = accountcollect.get(s);
            for (ThirdContentLink thirdContentLink : thirdContentLinks) {
                thirdIs.add(thirdContentLink.getId());
            }

            ThirdAccount thirdAccount = thirdAccountService.getById(s);
            ThirdInfo thirdInfo = thirdInfoService.getById(thirdAccount.getBaseId());
            String token = WeiBoUtil.getAccessToken(thirdInfo.getClientKey(), thirdInfo.getClientSecret(), thirdAccount.getAuthorizationCode());

//            String token =  douYinInfo.get("access_token");
//            String openId = douYinInfo.get("open_id");
//        String weiBoToken = WeiBoUtil.getAccessToken();

            for (ThirdContentLink thirdContentLink : accountcollect.get(s)) {
                //{
                //        "id": "32817222",
                //        "comments": "16",
                //        "reposts": "38",
                //        "likes": "38"
                //    },
                try {
                    String url = "https://c.api.weibo.com/2/statuses/statistic/biz.json?source=source&access_token" + token + "?id" + thirdContentLink.getId() + "?starttime" + endTime + "?endtime" + endTime;

                    String result = HttpUtils.get(url);
                    JSONObject jsonObject = JSON.parseObject(result);
                    JSONArray jsonArray = jsonObject.getJSONArray(result);
                    Integer readCount = 0;
                    for (Object o : jsonArray) {
                        JSONObject detail = JSON.parseObject(o.toString());
                        readCount = detail.getInteger("receive_read_count");

                    }

                    //api 地址  https://open.weibo.com/wiki/C/2/statuses/count/biz
                    String countUrl = "https://c.api.weibo.com/2/statuses/count/biz.json?access_token=" + token + "&ids=" + thirdContentLink.getId();

                    String countresult = HttpUtils.get(countUrl);
                    JSONObject countjsonObject = JSON.parseObject(countresult);
                    Integer reposts = countjsonObject.getInteger("reposts");
                    Integer likes = countjsonObject.getInteger("likes");


                    ThirdReadNum thirdReadNum = new ThirdReadNum();
                    thirdReadNum.setReadNum(readCount);
                    thirdReadNum.setContentId(thirdContentLink.getContentId());
                    thirdReadNum.setRepostsNum(reposts);
                    thirdReadNum.setLoveNum(likes);
                    thirdReadNum.setType(2);
                    thirdReadNumService.saveOrUpdate(thirdReadNum);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }


    }
}
